package br.com.educa.modelo.financeiro;

import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.PersistenceContext;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;
import javax.validation.constraints.NotNull;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.json.RooJson;
import org.springframework.roo.addon.serializable.RooSerializable;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.transaction.annotation.Transactional;

@Entity
@Table
@Configurable
@RooSerializable
@RooJson
@RooJavaBean
@RooToString
@RooJpaActiveRecord
public class HistoricoCheque implements Serializable {

	@PersistenceContext
    transient EntityManager entityManager;

	public static final EntityManager entityManager() {
        EntityManager em = new HistoricoCheque().entityManager;
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }

	public static long countHistoricoCheques() {
        return entityManager().createQuery("SELECT COUNT(o) FROM HistoricoCheque o", Long.class).getSingleResult();
    }

	public static List<HistoricoCheque> findAllHistoricoCheques() {
        return entityManager().createQuery("SELECT o FROM HistoricoCheque o", HistoricoCheque.class).getResultList();
    }

	public static HistoricoCheque findHistoricoCheque(Long id) {
        if (id == null) return null;
        return entityManager().find(HistoricoCheque.class, id);
    }

	public static List<HistoricoCheque> findHistoricoChequeEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("SELECT o FROM HistoricoCheque o", HistoricoCheque.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

	@Transactional
    public void persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.persist(this);
    }

	@Transactional
    public void remove() {
        if (this.entityManager == null) this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
            HistoricoCheque attached = HistoricoCheque.findHistoricoCheque(this.id);
            this.entityManager.remove(attached);
        }
    }

	@Transactional
    public void flush() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.flush();
    }

	@Transactional
    public void clear() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.clear();
    }

	@Transactional
    public HistoricoCheque merge() {
        if (this.entityManager == null) this.entityManager = entityManager();
        HistoricoCheque merged = this.entityManager.merge(this);
        this.entityManager.flush();
        return merged;
    }

	@Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

	@Version
    @Column(name = "version")
    private Integer version;
	
	@ManyToOne
	@JoinColumn(name = "cheque", referencedColumnName = "id")
	private Cheques cheque;
	
    @Temporal(TemporalType.DATE)
    @DateTimeFormat(style = "M-")
    private Date dataHistorico;
    
    @ManyToOne
    @JoinColumn(name = "caixa", referencedColumnName = "id")
    private Caixas caixa;
    
    @ManyToOne
    @JoinColumn(name = "conta", referencedColumnName = "id")
    private PlanoContas conta;

	public Long getId() {
        return this.id;
    }

	public void setId(Long id) {
        this.id = id;
    }

	public Integer getVersion() {
        return this.version;
    }

	public void setVersion(Integer version) {
        this.version = version;
    }

	public String toString() {
        return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

	private static final long serialVersionUID = 1L;

	public Cheques getCheque() {
        return this.cheque;
    }

	public void setCheque(Cheques cheque) {
        this.cheque = cheque;
    }

	public Date getDataHistorico() {
        return this.dataHistorico;
    }

	public void setDataHistorico(Date dataHistorico) {
        this.dataHistorico = dataHistorico;
    }

	public Caixas getCaixa() {
        return this.caixa;
    }

	public void setCaixa(Caixas caixa) {
        this.caixa = caixa;
    }

	public PlanoContas getConta() {
        return this.conta;
    }

	public void setConta(PlanoContas conta) {
        this.conta = conta;
    }

	public String toJson() {
        return new JSONSerializer().exclude("*.class").serialize(this);
    }

	public static HistoricoCheque fromJsonToHistoricoCheque(String json) {
        return new JSONDeserializer<HistoricoCheque>().use(null, HistoricoCheque.class).deserialize(json);
    }

	public static String toJsonArray(Collection<HistoricoCheque> collection) {
        return new JSONSerializer().exclude("*.class").serialize(collection);
    }

	public static Collection<HistoricoCheque> fromJsonArrayToHistoricoCheques(String json) {
        return new JSONDeserializer<List<HistoricoCheque>>().use(null, ArrayList.class).use("values", HistoricoCheque.class).deserialize(json);
    }
}
